import * as textInput from './textInput/utils'
import * as textAreaInput from './textAreaInput/utils'
import * as numberInput from './numberInput/utils'
import * as singleSelect from './singleSelect/utils'
import * as multipleSelect from './multipleSelect/utils'
import * as dateSelect from './dateSelect/utils'
import * as dateRangeSelect from './dateRangeSelect/utils'
import * as imageUpload from './imageUpload/utils'
import * as fileUpload from './fileUpload/utils'
import * as userSelect from './userSelect/utils'
import * as remarkText from './remarkText/utils'
import * as linkText from './linkText/utils'
import * as innerTable from './innerTable/utils'
import * as countData from './countData/utils'
import * as formatData from './formatData/utils'
import * as serverData from './serverData/utils'

import type { FormItem, FormValueItem } from '../types'

// 初始化表单项配置
export const initFormConfig = (formItem: FormItem) => {
  if (formItem.formType === 'TextInput') {
    return textInput.initFormConfig(formItem)
  }
  if (formItem.formType === 'TextAreaInput') {
    return textAreaInput.initFormConfig(formItem)
  }
  if (formItem.formType === 'NumberInput') {
    return numberInput.initFormConfig(formItem)
  }
  if (formItem.formType === 'SingleSelect') {
    return singleSelect.initFormConfig(formItem)
  }
  if (formItem.formType === 'MultipleSelect') {
    return multipleSelect.initFormConfig(formItem)
  }
  if (formItem.formType === 'DateSelect') {
    return dateSelect.initFormConfig(formItem)
  }
  if (formItem.formType === 'DateRangeSelect') {
    return dateRangeSelect.initFormConfig(formItem)
  }
  if (formItem.formType === 'ImageUpload') {
    return imageUpload.initFormConfig(formItem)
  }
  if (formItem.formType === 'FileUpload') {
    return fileUpload.initFormConfig(formItem)
  }
  if (formItem.formType === 'UserSelect') {
    return userSelect.initFormConfig(formItem)
  }
  if (formItem.formType === 'RemarkText') {
    return remarkText.initFormConfig(formItem)
  }
  if (formItem.formType === 'LinkText') {
    return linkText.initFormConfig(formItem)
  }
  if (formItem.formType === 'InnerTable') {
    return innerTable.initFormConfig(formItem)
  }
  if (formItem.formType === 'CountData') {
    return countData.initFormConfig(formItem)
  }
  if (formItem.formType === 'FormatData') {
    return formatData.initFormConfig(formItem)
  }
  if (formItem.formType === 'ServerData') {
    return serverData.initFormConfig(formItem)
  }
  return {}
}

// 初始化表单项输出参数
export const initFormValue = (formItem: FormItem) => {
  const valueObj: FormValueItem = {
    form_input_id: formItem.config.key || '',
    form_input_name: formItem.config.title || '',
    form_input_type: formItem.formType,
    form_input_value: '',
    key: formItem.config.key || ''
  }
  if (formItem.formType === 'NumberInput') {
    valueObj.form_input_value = undefined
  }
  if (formItem.formType === 'DateRangeSelect') {
    valueObj.form_input_value = '[]'
  }
  if (formItem.formType === 'UserSelect') {
    valueObj.form_input_value = '[]'
  }
  return valueObj
}

// 表单校验
export const validateFormValue = (formValueList: FormValueItem[], formItemList: FormItem[]) => {
  for (let i = 0; i < formValueList.length; i++) {
    // 表单项配置
    const formValue = formValueList[i].form_input_value
    const formItem = formItemList.find(
      (el: FormItem) => el.config.key === formValueList[i].key
    ) as FormItem
    const formConfig = formItem.config || {}
    // 表单项必填 且 流程节点配置可编辑
    if (formConfig.required) {
      if (formValue === '' || formValue === undefined || formValue === '[]') {
        return `请完善${formConfig.title}信息`
      }
    }
    // 多行表格组件自定义校验
    if (formItem.formType == 'InnerTable') {
      const errorStr = innerTable.validateFormValue(formValue, formConfig)
      if (errorStr) {
        return errorStr
      }
    }

    if (formItem.formType === 'NumberInput') {
      const errorStr = numberInput.validateFormValue(formValue, formConfig)
      if (errorStr) {
        return errorStr
      }
    }
  }
  return false
}

// 获取数据源的输出值
export const getInputValue = (
  type: string,
  formValue: {
    form_input_value: string | number
  },
  formConfig: any
) => {
  let result: number | string | undefined = undefined
  if (type === 'InnerTable') {
    const value = formValue.form_input_value as string
    const data = value ? JSON.parse(value) : []
    result = innerTable.exportValFn(data, formConfig)
  } else {
    result = formValue.form_input_value
  }
  return result
}
